tests: Add pthread based concurrency stress test for babl_fish()
authorMartin Nordholts <martinn@src.gnome.org>
Thu, 19 Nov 2009 18:59:10 +0000 (19:59 +0100)
committerMartin Nordholts <martinn@src.gnome.org>
Thu, 19 Nov 2009 19:25:53 +0000 (20:25 +0100)
Add a test that runs a bunch of pthread threads, all trying to use
babl_fish() at the same time. This test currently fails and it is the
goal of bug 587675 to make it pass.

The cause of the failure is the static variables in babl-fish-path.c.
In particular 'current_path' clearly needs to be moved to some
instance variable.

tests/.gitignore
tests/Makefile.am
tests/concurrency-stress-test.c [new file with mode: 0644]

index be9ba90860dea3815aa89a54d4451b45e3fe451a..dabfa78c92ce50408bcdcfaf31e3e76481fd2d74 100644 (file)
@@ -6,6 +6,7 @@
 /babl_class_name*
 /babl_fish_path_dhtml*
 /babl_fish_path_fitness*
+/concurrency-stress-test*
 /conversions*
 /float_to_u8
 /formats*
index 360906b00a299891d3fcf3996bc0594ec60f0fbd..5b06374c969e2ee01d88c702417f2caa9c5b4f36 100644 (file)
@@ -6,11 +6,13 @@ TESTS =                               \
        sanity                  \
        babl_class_name         \
        types                   \
-       models  
+       models                  \
+       concurrency-stress-test 
 
 TESTS_ENVIRONMENT = BABL_PATH=$(top_builddir)/extensions/.libs
 
 AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/babl
+AM_LDFLAGS  = -pthread
 
 LDADD = $(top_builddir)/babl/libbabl-@BABL_API_VERSION@.la \
        $(MATH_LIB)
diff --git a/tests/concurrency-stress-test.c b/tests/concurrency-stress-test.c
new file mode 100644 (file)
index 0000000..20d31b4
--- /dev/null
@@ -0,0 +1,82 @@
+/* babl - dynamically extendable universal pixel conversion library.
+ * Copyright (C) 2009 Martin Nordholts
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "config.h"
+
+#include <math.h>
+#include <pthread.h>
+
+#include "babl.h"
+
+
+#define N_THREADS               10
+#define N_ITERATIONS_PER_THREAD 100
+
+
+static void *
+babl_fish_path_stress_test_thread_func (void *not_used)
+{
+  int i;
+
+  for (i = 0; i < N_ITERATIONS_PER_THREAD; i++)
+    {
+      /* Try to get a fish with an as complex conversion path as
+       * possible
+       */
+      Babl *fish = babl_fish ("R'G'B'A u16", "YA double");
+
+      /* Just do something random with the fish */
+      babl_get_name (fish);
+    }
+
+  return NULL;
+}
+
+int
+main (int    argc,
+      char **argv)
+{
+  pthread_t threads[N_THREADS];
+  int       i;
+
+  babl_init ();
+
+  /* Run a few threads at the same time */
+  for (i = 0; i < N_THREADS; i++)
+    {
+      pthread_create (&threads[i],
+                      NULL, /* attr */
+                      babl_fish_path_stress_test_thread_func,
+                      NULL /* arg */);
+     }
+
+  /* Wait for them all to finish */
+  for (i = 0; i < N_THREADS; i++)
+    {
+      pthread_join (threads[i],
+                    NULL /* thread_return */);
+    }
+
+  babl_exit ();
+
+  /* If we didn't crash we assume we're OK. We might want to add more
+   * asserts in the test later
+   */
+  return 0;
+}